<%--
  Created by IntelliJ IDEA.
  User: Deja Vu
  Date: 2020/12/14
  Time: 20:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>accounts</title>
    <link rel="stylesheet" href="layui/css/layui.css" media="all">
    <script src="js/jquery-1.12.4.js"></script>
</head>
<body>
<form id="addform" lay-filter="addform" class="layui-form" > <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-form-item">
        <label class="layui-form-label">书名</label>
        <div class="layui-input-block">

            <input type="text" name="bookName" placeholder="请输入图书名称" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">图片</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="addImg">
                <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
            <img id="img" />
            <input type="hidden" name="productImage"class="layui-input" id="img22" name="productImage">

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-block">
            <input type="text" name="bookPrice" placeholder="请输入价格" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">作者</label>
        <div class="layui-input-block">
            <input type="text" name="author" placeholder="请填写作者" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">库存</label>
        <div class="layui-input-block">
            <input type="text" name="inventory" placeholder="请填库存数量" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">类别</label>
        <div class="layui-input-block">
            <%--下拉列表框--%>
            <select name="cid">

            </select>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">上架</label>
        <div class="layui-input-block">
            <%--下拉列表框--%>
            <select name="bookState">
                <option value="onsale">上架</option>
                <option value="1">下架</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="go">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script type="text/javascript" src="layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['table','form','upload'],function () {
        var form = layui.form;
        var table = layui.table;
        var upload = layui.upload;
        $.ajax({
            url: "categorys",
            type: "GET",
            contentType: "application/json",
            datatype: "json",
            success: function (data) {
                var s = "";
                for (var i = 0; i < data.length; i++) {
                    s += "<option value='" + data[i].categoryId + "'>" + data[i].categoryName + "</option>";
                    console.log(data.categoryId)
                }

                $("[name='cid']").html(s);
                form.render("select");
            }
        })
        var uploadInst = upload.render({
            elem: '#addImg' //绑定元素
            ,url: 'upload' //上传接口
            ,done: function(res){
                //上传完毕回调
                    $("#img").attr("src",res.msg);
                    $("#img22").val(res.msg);
            }
    });
        form.on('submit(go)', function(data){
            var fieid = data.field; //当前容器的全部表单字段，名值对形式：{name: value}
            console.log(fieid.bookName)
            $.ajax({
                url:"${pageContext.request.contextPath}/productes",
                data:JSON.stringify(fieid),
                type:"post",
                contentType:"application/json",
                success:function (res) {
                    console.log(fieid);
                    layer.msg("添加成功");
                    $("#img").get(0).reset();
                    $("#addform").get(0).reset();
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    })

</script>
</body>
</html>
